def get_min_near_loc(role_loc, target_loc_list):
    """
    target_loc_list必须有元素
    """
    min = 99999
    min_x = 0
    min_y = 0
    for target_loc in target_loc_list:
        value = abs(role_loc[0] - target_loc[0]) + abs(role_loc[1] - target_loc[1])
        if value < min:
            min = value
            min_x = target_loc[0]
            min_y = target_loc[1]
    return (min_x, min_y)


def get_near_type_loc(role_loc, enemys, pick_ups, default_type=1):
    """
    default_type: 1 优先选择最近目标，2 优先选择拾取物
    """
    enemy_loc = get_min_near_loc(role_loc, enemys)
    pick_up_loc = get_min_near_loc(role_loc, pick_ups)
    if len(enemys) == 0:
        return False, pick_up_loc
    if len(pick_ups) == 0:
        return True, enemy_loc

    if default_type == 2:
        return False, pick_up_loc

    enemy_value = abs(role_loc[0] - enemy_loc[0]) + abs(role_loc[1] - enemy_loc[1])
    pick_up_value = abs(role_loc[0] - pick_up_loc[0]) + abs(role_loc[1] - pick_up_loc[1])
    if enemy_value > pick_up_value:
        return False, pick_up_loc
    else:
        return True, enemy_loc


def get_max_far_loc(role_loc, target_loc_list):
    max = 0
    max_x = 0
    max_y = 0
    for target_loc in target_loc_list:
        value = abs(role_loc[0] - target_loc[0]) + abs(role_loc[1] - target_loc[1])
        if value > max:
            max = value
            max_x = target_loc[0]
            max_y = target_loc[1]
    return (max_x, max_y)


def get_offset_with_name(file_name):
    if contains_chars('[', file_name) and contains_chars(']', file_name):
        start = file_name.index('[')
        end = file_name.index(']')
        loc_str = file_name[int(start) + 1:int(end)]
        lst = loc_str.split(',')
        return (int(lst[0]), int(lst[1]))
    else:
        return (0, 0)


def contains_chars(s, chars):
    return any(c in s for c in chars)


if __name__ == '__main__':
    print(get_offset_with_name("r1[455,-60].png"))
